'use strict';

exports.main = async (event, context) => {
  const db = uniCloud.database();
  
  try {
    // 获取最新的5条任务，不进行任何筛选
    const res = await db.collection('taskList')
      .orderBy('publish_time', 'desc')
      .limit(5)
      .get();
    
    console.log('[debugLatestTasks] 最新5条任务:', res.data);
    
    // 格式化输出
    const formattedTasks = res.data.map(task => ({
      _id: task._id,
      title: task.title,
      status: task.status,
      type: task.type,
      publish_time: task.publish_time,
      publisher_id: task.publisher_id
    }));
    
    return {
      code: 200,
      msg: 'success',
      data: formattedTasks,
      total: res.data.length
    };
  } catch (error) {
    console.error('[debugLatestTasks] 错误:', error);
    return {
      code: 500,
      msg: '获取任务失败',
      error: error.message
    };
  }
};